# Maintainer: Matthias Gondan <Matthias.Gondan-Rochon@uibk.ac.at>

_realname=swi-prolog
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.0.3
pkgrel=1
pkgdesc="Prolog environment (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'mingw32' 'ucrt64' 'clang64' 'clang32')
url="https://www.swi-prolog.org/"
license=("FreeBSD")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-gmp: support for large integers"
            "${MINGW_PACKAGE_PREFIX}-libyaml: yaml support"
            "${MINGW_PACKAGE_PREFIX}-db: support for Berkeley db"
            "${MINGW_PACKAGE_PREFIX}-pcre: support for regular expressions"
            "${MINGW_PACKAGE_PREFIX}-xpm-nox: for the graphical user interface"
            "${MINGW_PACKAGE_PREFIX}-openjpeg: for the graphical user interface")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gmp"
             "${MINGW_PACKAGE_PREFIX}-libarchive"
             "${MINGW_PACKAGE_PREFIX}-libyaml"
             "${MINGW_PACKAGE_PREFIX}-db"
             "${MINGW_PACKAGE_PREFIX}-pcre"
             "${MINGW_PACKAGE_PREFIX}-xpm-nox"
             "${MINGW_PACKAGE_PREFIX}-openjpeg2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("https://www.swi-prolog.org/download/stable/src/swipl-${pkgver}.tar.gz"
        "01-skip-installation-of-system-dlls.patch"
        "02-unixdirs-cmakelist.patch"
        "03-unixdirs-src-cmakelist.patch"
        "04-unixdirs-boot-init.patch"
        "05-unixdirs-library-prolog_config.patch"
        "06-unixdirs-build-tools.patch")

sha256sums=('e2919bc58710abd62b9cd40179a724c30bdbe9aa428af49d7fdc6d0158921afb'
            'cc1af3831f0fec88dcd626b9c58eb73a35190fe9f6483149a20a60bbbe5374f4'
            '30d729e5ea584e4f7c134f5fac2bc53d61081b0dd1f9812ba3bb0941ebb50b22'
            '6df74b1780dd75f01fcea47fac0d54446bd61293bc2840b360269ffe6252f0e2'
            'a477651e07e86285d4bd584f30801293c4e4dccecde1fe02d96ee3f859596c01'
            '3cba330711c1bc3779326c17b188a5e8dc02b2da280c3dc0a46ba430d4a08f58'
            'b044d0457b37b59aa3f74631e6a6ebd2c29352debfe8fae0de178aeb44d383c4')

prepare() {
  cd ${srcdir}/swipl-${pkgver}
  patch -p1 < ${srcdir}/01-skip-installation-of-system-dlls.patch
  patch -p1 < ${srcdir}/02-unixdirs-cmakelist.patch
  patch -p1 < ${srcdir}/03-unixdirs-src-cmakelist.patch
  patch -p1 < ${srcdir}/04-unixdirs-boot-init.patch
  patch -p1 < ${srcdir}/05-unixdirs-library-prolog_config.patch
  patch -p1 < ${srcdir}/06-unixdirs-build-tools.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DMSYS2=1 \
    -DMINGW_ROOT=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DWIN32_DLLS="" \
    -DSWIPL_PACKAGES_JAVA=OFF \
    -DINSTALL_DOCUMENTATION=OFF \
    -DSWIPL_{SHARED,STATIC}_LIB=ON \
    ../swipl-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  install -Dm644 "${srcdir}/swipl-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}
